import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
    public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    public static StringTokenizer st;

    public static String next() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }

        return st.nextToken();
    }

    public static String nextLine() throws IOException {
        return br.readLine();
    }

    public static boolean hasNext() throws IOException {
        return br.ready();
    }

    public static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }
   static boolean[][] vis;


    static List<int[]>[] list;
    static int n,m;
    public static void main(String[] args) throws IOException {
         n=nextInt();
         m=nextInt();
        list=new List[n+1];
        vis=new boolean[n+10][3010];


        for (int i=0;i<=n;i++) list[i]=new ArrayList<>();


        while (m-->0){
            int u=nextInt();
            int v=nextInt();
            int w=nextInt();
            list[u].add(new int[]{v,w});
            list[v].add(new int[]{u,w});

        }

        for(int i=1;i<=n;i++) bfs(i);

        System.out.println(ret);


    }

    static int[] dp;
    static int ret=0x3f3f3f3f;



    public static void bfs(int pos){
        Queue<int[]> q=new LinkedList<>();
        dp=new int[n+1];
        Arrays.fill(dp,0x3f3f3f3f);
        dp[pos]=0x3f3f3f3f;
        q.add(new int[]{pos,0});

        while(!q.isEmpty()){

            int[] arr=q.poll();
            int v=arr[0];
            int w=arr[1];


            if(vis[v][w]) continue;
            vis[v][w]=true;

            for(int arr1[]:list[v]){
                q.add(new int[]{arr1[0],w^arr1[1]});
                dp[arr1[0]]=Math.min(dp[arr1[0]],w^arr1[1]);
            }

        }
        for(int i=1;i<=n;i++){
            if(i==pos) continue;
            ret=Math.min(ret,dp[i]);
        }


    }
}
